package com.hss.common.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeTools {
	/**
	 * 求两个日期之间的   相差天数
	 * @param time
	 * @return
	 */
	public static long  getFromCurrentDate2TimeDays(String time){
		long quot = 0;
		  SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
		  try {
		   Date date1 = ft.parse( time );
		   Date date2 = new Date();
		   quot =   date2.getTime() - date1.getTime();
		   quot = quot / 1000 / 60 / 60 / 24;
		  } catch (ParseException e) {
		   e.printStackTrace();
		  }
		  return quot;
	}
	
	public static long  getFromCurrentDate2TimeDays(Date date1){
		long quot = 0;
		   Date date2 = new Date();
		   quot =   date2.getTime() - date1.getTime(); 
		   
		   quot = Math.abs(quot);
		   quot = quot / 1000 / 60 / 60 / 24;
		   
		   
		  return quot;
	}
	
	public static long getQuot(String time1, String time2){
		  long quot = 0;
		  SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
		  try {
		   Date date1 = ft.parse( time1 );
		   Date date2 = ft.parse( time2 );
		   quot = date1.getTime() - date2.getTime();
		   quot = quot / 1000 / 60 / 60 / 24;
		  } catch (ParseException e) {
		   e.printStackTrace();
		  }
		  return quot;
		 }

    /**
     * 获取系统当前时间
     * @return
     */
    public static String getSystemDate(){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ft.format(new Date());
    }
    
    /**
     * 
     * @ 功能  获取当前day
     * @return
     * @ 作者  2014-8-15  / 黄赛赛
     */
    public static String getCurDayDate(){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        return ft.format(new Date());
    }
    /**
     * 获取系统当前时间
     * @return
     */
    public static long getSystemSeconds(){
    	Date d = new Date();
    	return d.getTime();
    }
    
    public static String getFormatDate(Date date){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ft.format(date);
    }
    public static String getFormatDateDay(Date date){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        return ft.format(date);
    }
    
    public static String getFormatDateDayCN(Date date){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy年MM月dd日");
        return ft.format(date);
    }
    public static String getFormatDateMinute(Date date){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return ft.format(date);
    }
    public static Date getDate(String time){
    	Date date = null;
    	SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
			date = ft.parse( time );
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return date;
    }


	/**
	 *时间转换成多少分钟之前
	 *  @param timeStr 时间
	 * @return String
	 */
	public static String getStandardDate(String timeStr) {
		StringBuilder sb = new StringBuilder();
		long t = Long.parseLong(timeStr);
		// long time = System.currentTimeMillis() - (t * 1000);
		long time = System.currentTimeMillis() - t;
		long mill = (long) Math.ceil(time / 1000);// 秒前
		long minute = (long) Math.ceil(time / 60 / 1000.0f);// 分钟前
		long hour = (long) Math.ceil(time / 60 / 60 / 1000.0f);// 小时
		long day = (long) Math.ceil(time / 24 / 60 / 60 / 1000.0f);// 天前
		if (day - 1 > 0) {
			sb.append(day).append("天");
		} else if (hour - 1 > 0) {
			if (hour >= 24) {
				sb.append("1天");
			} else {
				sb.append(hour).append("小时");
			}
		} else if (minute - 1 > 0) {
			if (minute == 60) {
				sb.append("1小时");
			} else {
				sb.append(minute).append("分钟");
			}
		} else if (mill - 1 > 0) {
			if (mill == 60) {
				sb.append("1分钟");
			} else {
				sb.append(mill).append("秒");
			}
		} else {
			sb.append("刚刚");
		}
		if (!sb.toString().equals("刚刚")) {
			sb.append("前");
		}
		return sb.toString();
	}

	/**
	 * 通过秒数换算出 分 秒
	 *
	 * @param miao long
	 * @return String
	 */
	public static String getGeShiDate(long miao) {
		long minute = miao / 60;
		long scond = miao - minute * 60;
		return minute + "分" + scond + "秒";
	}
	/**
	 * 毫秒值转换成 年月日时分秒 getDifference(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 *
	 * @param period long
	 * @return String
	 */
	private static String getDifference(long period) {
		String result;

		/******* 计算出时间差中的年、月、日、天、时、分、秒 *******/
		int year = getYear(period);
		int month = getMonth(period - year * yearLevelValue);
		int day = getDay(period - year * yearLevelValue - month * monthLevelValue);
		int hour = getHour(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue);
		int minute = getMinute(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue - hour * hourLevelValue);
		int second = getSecond(period - year * yearLevelValue - month * monthLevelValue - day * dayLevelValue - hour * hourLevelValue - minute * minuteLevelValue);

		result = year + "年" + month + "月" + day + "天" + hour + "时" + minute + "分" + second + "秒";
		return result;
	}

	public static int getYear(long period) {
		return (int) (period / yearLevelValue);
	}

	public static int getMonth(long period) {
		return (int) (period / monthLevelValue);
	}

	public static int getDay(long period) {
		return (int) (period / dayLevelValue);
	}

	public static int getHour(long period) {
		return (int) (period / hourLevelValue);
	}

	public static int getMinute(long period) {
		return (int) (period / minuteLevelValue);
	}

	public static int getSecond(long period) {
		return (int) (period / secondLevelValue);
	}

	// 查分毫秒值用的常量
	private final static long yearLevelValue = 365 * 24 * 60 * 60 * 1000l;
	private final static long monthLevelValue = 30 * 24 * 60 * 60 * 1000l;
	private final static long dayLevelValue = 24 * 60 * 60 * 1000l;
	private final static long hourLevelValue = 60 * 60 * 1000l;
	private final static long minuteLevelValue = 60 * 1000l;
	private final static long secondLevelValue = 1000l;

	/**
	 * 比较两个时间点的差值 getDifference(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 *
	 * @param nowTime    long
	 * @param targetTime long
	 * @return String
	 */
	public static String getDifference(long nowTime, long targetTime) {// 目标时间与当前时间差
		long period = targetTime - nowTime;
		return getDifference(period);
	}

	/**
	 * 得到闹钟的时间
	 *
	 * @param sd 时间 毫秒
	 * @return String 日期
	 */
	public static String getDateToString(long sd) {
		// long sd=1345185923140L;
		Date dat = new Date(sd);
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(dat);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format.format(gc.getTime());
	}

	/**
	 * 得到倒计时的秒值
	 *
	 * @param strDate String
	 * @return long
	 */
	public static long getDaojiTime(String strDate) {
		long i = 0;
		Calendar r = Calendar.getInstance();
		Calendar r1 = Calendar.getInstance();// 创建Calendar对象初始化为系统当前值
		try {
			// String str = "2013-05-23 15:36:12";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date birthday = sdf.parse(strDate);
			r.setTime(birthday);// 用获得的Date设置r
			Date t = r.getTime();
			i = (r1.getTimeInMillis() - r.getTimeInMillis()) / 1000;// 进行比较,两者具体相差多少毫秒
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return i;
	}

	/**
	 * 通过参数传入1-7 返回周几
	 *
	 * @param sdate String
	 * @return String
	 */
	public static String getWeekStr(String sdate) {
		String str;
		str = sdate;
		if ("1".equals(str)) {
			str = "周日";
		} else if ("2".equals(str)) {
			str = "周一";
		} else if ("3".equals(str)) {
			str = "周二";
		} else if ("4".equals(str)) {
			str = "周三";
		} else if ("5".equals(str)) {
			str = "周四";
		} else if ("6".equals(str)) {
			str = "周五";
		} else if ("7".equals(str)) {
			str = "周六";
		}
		return str;
	}

	/**
	 * 得到当前时间 yy-mm-dd HH:mm:ss
	 *
	 * @return str 和服务端发送数据一致
	 */
	public static String Getnowtime() {
		Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH) + 1;
		int date1 = c.get(Calendar.DATE);
		int hour1 = c.get(Calendar.HOUR_OF_DAY);
		int minute1 = c.get(Calendar.MINUTE);
		int second1 = c.get(Calendar.SECOND);
		return year1 + "-" + NumDouble(month1) + "-" + NumDouble(date1) + " " + NumDouble(hour1) + ":" + NumDouble(minute1) + ":" + NumDouble(second1);
	}

	public static String strToDate(String strDate) throws ParseException {
		int a;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date strtodate = formatter.parse(strDate);
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(strtodate);
		a = cal1.get(Calendar.DAY_OF_WEEK);
		return a + "";
	}

	/**
	 * 将时间转换成 24时格式
	 *
	 * @param timea  十二小时格式
	 * @return  返回值
	 */
	private static String NumDouble(int timea) {
		DecimalFormat df = new DecimalFormat("00");
		return df.format(timea);
	}



	/**
	 * 得到最终显示值。eg:凌晨，中午，昨天，11月7日，2014年8月23日
	 *
	 * @param time String
	 * @return str
	 *
	 */
	public static String GetDateResult(String time) {
		if (time != null && !time.equals("")) {
			// 2011 11 21 12 12 12yyyyMMddhhmmss //2013 03 12 12 50 01
			int year = Integer.valueOf((String) time.subSequence(0, 4));
			int month = Integer.valueOf((String) time.subSequence(4, 6));
			int date = Integer.valueOf((String) time.subSequence(6, 8));
			int hour = Integer.valueOf((String) time.subSequence(8, 10));
			int minute = Integer.valueOf((String) time.subSequence(10, 12));
			// int second=Integer.valueOf((String) time.subSequence(12, 14));
			Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改
			int year1 = c.get(Calendar.YEAR);
			// int month1 = c.get(Calendar.MONTH)+1;
			// int date1 = c.get(Calendar.DATE);
			int data11 = c.get(Calendar.DAY_OF_YEAR);
			// int hour1 = c.get(Calendar.HOUR_OF_DAY);
			// int minute1 = c.get(Calendar.MINUTE);
			// int second1 = c.get(Calendar.SECOND);
			String dayofyear = year + "/" + month + "/" + date;
			String str = null;
			int dataM = Math.abs(data11 - GetDayOfYear(dayofyear));
			int yearM = Math.abs(year1 - year);
			if (dataM == 0) {// 今天
				if (hour >= 0 && hour < 5) {// 凌晨
					str = "凌晨";
				} else if (hour >= 5 && hour < 9) {// 早晨
					str = "早晨";
				} else if (hour >= 9 && hour < 13) {// 上午
					str = "上午";
				} else if (hour >= 13 && hour < 19) {// 下午
					str = "下午";
				} else if (hour >= 18 && hour < 21) {// 晚上
					str = "晚上";
				} else if (hour >= 21 && hour <= 24) {// 深夜
					str = "深夜";
				}
			} else if (dataM == 1) { // 昨天
				str = "昨天";
			} else if (dataM == 2) {// 周几
				str = Week(time);
			} else {// 年月日
				if (yearM == 0) { // 月 日
					str = month + "月" + date + "日";
				} else {// 年月日
					str = year + "年" + month + "月" + date + "日";
				}
			}
			return str + "  " + NumDouble(hour) + ":" + NumDouble(minute);
		} else
			return "";
	}

	private static int GetDayOfYear(String time) {
		@SuppressWarnings("deprecation")
		Date date = new Date(time);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	public static String Week(String time) {
		final String dayNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		try {
			date = sdfInput.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0)
			dayOfWeek = 0;
		return dayNames[dayOfWeek];
	}

}
